package com.p2p.front.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.p2p.base.model.Money;
import com.p2p.base.util.DateUtil;
import com.p2p.front.dal.dao.FinanceDao;
import com.p2p.front.dal.dao.TenderDao;
import com.p2p.front.dal.model.ChargeLogSummary;
import com.p2p.front.dal.model.Finance;
import com.p2p.front.enums.ChargeType;
import com.p2p.front.enums.TenderStatus;
import com.p2p.front.service.ChargeLogSummaryService;
import com.p2p.front.service.FinanceService;
import com.p2p.front.service.LoanInfoService;
import com.p2p.front.service.TenderReturnService;
import com.p2p.front.vo.FinanceVO;

@Service("financeService")
public class FinanceServiceImpl implements FinanceService {

	@Resource
	private FinanceDao financeDao;

	@Resource
	private LoanInfoService loanInfoService;

	@Resource
	private TenderReturnService tenderReturnService;

	@Resource 
	private TenderDao tenderDao;
	
	@Resource
	private ChargeLogSummaryService chargeLogSummaryService;
	@Override
	public Date getLastUpdateTime() {
		Finance f = financeDao.findFineceInfo();
		if (f != null)
			return f.getUpdateTime();
		else
			return new Date();
	}

	@Override
	public void saveFinanceInfo(Money dayAmount, Money dayEarnings, String date) {
		// TODO Auto-generated method stub
		financeDao.saveFinanceInfo(dayAmount, dayEarnings, date);

	}

	@Override
	public Finance getLatestFinance() {
		// TODO Auto-generated method stub
		return financeDao.getLatestFinance();
	}

	@Override
	@Transactional
	public void getFinanceData(String date) throws ParseException {
		Date dateBegin = DateUtil.parseDateTime(date + " 00:00:00");
		Date dateEnd = DateUtil.parseDateTime(date + " 23:59:59");
		// 计算昨天总交易记录和投资人总收益
		Long dayAmount = loanInfoService.getDayAmount(dateBegin, dateEnd);
		if (dayAmount == null) {
			dayAmount = 0l;
		}
		Long dayEarnings = tenderReturnService.getDayEarnings(dateBegin, dateEnd);
		if (dayEarnings == null||dayEarnings<0l) {
			dayEarnings = 0l;
		}
		
		financeDao.deleteByDate(date);
		financeDao.saveFinanceInfo(new Money(dayAmount),
				new Money(dayEarnings), date);
	}

	@Override
	public FinanceVO getTotleFinance() {
		// TODO Auto-generated method stub
		return financeDao.getTotleFinance();
	}

	//TODO 优化，首页展示的几项信息，没有必要查询这么多
	@Override
	public Map<String, Object> myFinance(Integer uid) {

		// 已收本金
		Integer backPrincipal = 0;
		// 待回收本金
		Integer toBackPrincipal = 0;

		// 已回收本息
		Integer backAmount = 0;

		// 待回收本息
		Integer toBackAmount =0;
		
		// 已回收笔数
		Integer backTime = 0;
		// 待回收笔数
		Integer toBackTime = 0;

		List<Map<String, Object>> list = tenderReturnService.listAll(uid);

		for (Map<String, Object> map : list) {
			Integer status = Integer.parseInt(map.get("status").toString());
			Integer principal = Integer.parseInt(map.get("principal").toString());
			Integer amount = Integer.parseInt(map.get("amount").toString());
			//已回收
			if (status == 31) {
				backPrincipal+=principal;
				backAmount+=amount;
			} 
			//待回收
			else if (status == 21) {
				toBackPrincipal+= principal;
				toBackAmount+=amount;
			}
		}

		// 净赚利息
		Integer backInterest = backAmount-backPrincipal;
		// 待赚利息
		Integer toBackInterest = toBackAmount-toBackPrincipal;
		
		// 总借出总额
		Integer amount = backAmount + toBackAmount;
	
		Map<String, Object> finance = new HashMap<String, Object>();
	
		finance.put("backPrincipal",backPrincipal );
		finance.put("backAmount", backAmount);

		finance.put("toBackPrincipal",toBackPrincipal);
		finance.put("toBackAmount", toBackAmount);
		finance.put("backInterest", backInterest);
		finance.put("toBackInterest", toBackInterest);
		finance.put("amount", amount);
	
		toBackTime=tenderDao.countByStatus(uid.longValue(),TenderStatus.REAPYING.getCode());
		backTime=tenderDao.countByStatus(uid.longValue(),TenderStatus.REPAYED.getCode());
		// 总借出笔数
		Integer totalTime = backTime + toBackTime;
		
		finance.put("toBackTime",toBackTime );
		finance.put("backTime",backTime );
		finance.put("totalTime",totalTime );
		
		ChargeLogSummary chargeLogSummary=chargeLogSummaryService.get(uid.longValue(),ChargeType.INTEREST_MANAGEMENT_CHARGE);
		Money money = new Money(0L);
		if(chargeLogSummary!=null &&chargeLogSummary.getAmount()!=null){
			money=chargeLogSummary.getAmount();
		}
		finance.put("interestManagementAmount",money);
		return finance;
	}

}
